﻿using pdfer.FileObjects;
using pdfer.Syntax;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace pdfer.PdfObjecs.Fonts
{
    internal class CMAPDictionary: PdfFileObject
    {
        public CMAPDictionary(PdfFileObject pdfFileObject)
            : base(pdfFileObject)
        {
            if (pdfFileObject.TryGetValue(PdfName.Key_Type, out object? val))
            {
                Type = PdfParserUtils.ParseString(val);
                if (Type != PdfTypeConst.Type_CMap)
                {
                    throw new Exception("类型必须是 /CMap");
                }
            }
        }

        /// <summary>
        /// 必须是/Catalog
        /// (Required)
        /// </summary>
        public String Type { get; private set; }

        public String CMapName { get { return GetDicStringVal(PdfName.Key_CMapName); } }

        public CIDSystemInfoDictionary CIDSystemInfo
        {
            get
            {
                if (TryGetValue(PdfName.Key_CIDSystemInfo, out var obj))
                {
                    if (obj is String id)
                    {
                        return GetIndirectObj<CIDSystemInfoDictionary>(id);
                    }
                }
                return null;
            }
        }

        public Int32 WMode
        {
            get
            {
                return GetDicInt32Val(PdfName.Key_W2);
            }
        }

        /// <summary>
        /// 预定义的CMap 或者自定义的CMap
        /// </summary>
        public Object UseCMap
        {
            get
            {
                if(TryGetValue(PdfName.Key_UseCMap, out var obj))
                {
                    if(obj is String _str)
                    {
                        if (_str[0] == '/')
                        {
                            return _str;
                        }
                        else
                        {
                            return GetIndirectObj<CMAPDictionary>(_str);
                        }
                    }
                }
                return null;
            }
        }
    }
}
